USE [QuanLyHopDong]
GO

/****** Object:  View [dbo].[ThongKeHopDongChiTietView]    Script Date: 10/8/2014 12:22:16 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


ALTER view [dbo].[ThongKeHopDongChiTietView]
as
select HD.Id, HD.SoHopDong, HD.TenHopDong, HD.GiaTriHopDongChuaVAT, HD.TongGiaTriHopDong,
	DV.Ten as TenDonVi,
	QTNB.GiaTriQuyetToanNoiBo,
	QT.GiaTriQuyetToan, QT.NgayQuyetToan,
	CTT.SoTien as TongSoTienChuaThanhToan,
	DTT.SoTien as TongSoTienDaThanhToan,
	(QT.GiaTriQuyetToan - DTT.SoTien + CTT.SoTien) as SoTienConLai 
from HopDong HD
	left outer join DonVi DV on HD.DonViId = DV.Id
	left outer join QuyetToan QT on QT.HopDongId = HD.Id
	left outer join GiaTriQuyetToanNoiBoView QTNB on QTNB.HopDongId = HD.Id
	left outer join ChuaThanhToanView CTT on CTT.HopDongId = HD.Id
	left outer join DaThanhToanView DTT on DTT.HopDongId = HD.Id



GO


